package com.ysg.widget.title;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ysg.R;
import com.ysg.utils.YStringUtil;

/**
 * 标题栏
 */
public class YTitleBar extends RelativeLayout {
    public interface OnTitleBarListener {
        void onLeftClick();
        void onTitleClick();
        void onRightClick();
    }
    private OnTitleBarListener mListener;
    public void setOnTitleBarListener(OnTitleBarListener listener) {
        mListener = listener;
    }

    private TextView tvTitle, tvRight;
    private ImageView ivBack, ivRight;
    private View parent, viewLine, viewLeft, viewTitle, viewRight;

    public YTitleBar(Context context) {
        super(context);
    }

    public YTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        View view = View.inflate(context, R.layout.widget_title_bar, this);
        parent = view.findViewById(R.id.parent);
        viewLeft = view.findViewById(R.id.view_left);
        viewTitle = view.findViewById(R.id.view_title);
        ivBack = view.findViewById(R.id.ivBack);
        tvTitle = view.findViewById(R.id.tv_title);
        viewRight = view.findViewById(R.id.view_right);
        ivRight = view.findViewById(R.id.iv_right);
        tvRight = view.findViewById(R.id.tv_right);
        viewLine = view.findViewById(R.id.view_line);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.YTitleBar);
        boolean darkTheme = array.getBoolean(R.styleable.YTitleBar_darkTheme, false);
        boolean tbTransparent = array.getBoolean(R.styleable.YTitleBar_transparentBg, false);
        boolean leftVisible = array.getBoolean(R.styleable.YTitleBar_leftVisible, true);
        String title = array.getString(R.styleable.YTitleBar_title);
        int rightIcon = array.getResourceId(R.styleable.YTitleBar_rightIcon, 0);
        String rightText = array.getString(R.styleable.YTitleBar_rightText);
        boolean lineVisible = array.getBoolean(R.styleable.YTitleBar_lineVisible, false);
        array.recycle();

        if (darkTheme){
            ivBack.setImageResource(R.mipmap.ic_back_white);
            tvTitle.setTextColor(getResources().getColor(R.color.white));
            parent.setBackgroundResource(R.color.transparent);
        }

        if (tbTransparent){
            parent.setBackgroundResource(R.color.transparent);
        }

        viewLeft.setVisibility(leftVisible? View.VISIBLE: View.GONE);
        viewLine.setVisibility(lineVisible? View.VISIBLE: View.GONE);
        if (YStringUtil.isNotEmpty(title)){
            tvTitle.setText(title);
        }
        if (YStringUtil.isNotEmpty(rightText)){
            tvRight.setText(rightText);
        }
        if (0 == rightIcon && TextUtils.isEmpty(rightText)){
            viewRight.setVisibility(View.GONE);
        }
        if (0 != rightIcon){
            ivRight.setImageDrawable(getDrawable(context, rightIcon));
        }


        viewLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.onLeftClick();
                }
            }
        });
        viewTitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.onTitleClick();
                }
            }
        });
        viewRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.onRightClick();
                }
            }
        });

    }

    /**
     * 获取图片资源
     */
    public static Drawable getDrawable(Context context, int id) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return context.getResources().getDrawable(id, context.getTheme());
        } else {
            return context.getResources().getDrawable(id);
        }
    }

    public void setTitle(String title){
        tvTitle.setText(title);
    }
    public String getTitle(){
        return tvTitle.getText().toString();
    }
    public void setRightTitle(String title){
        tvRight.setText(title);
    }

    public void setLeftVisible(boolean b){
        viewLeft.setVisibility(b? VISIBLE: GONE);
    }
    public void setRightVisible(boolean b){
        viewRight.setVisibility(b? VISIBLE: GONE);
    }

    public View getRightView(){
        return viewRight;
    }




}
